<template>
  <div class='cate_container'>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <tree-table :data="cateList" :columns="columns" border :selection-type="false" index-text="#" show-index
        :expand-type="false" :show-row-hover="false">
        <template slot="isok" slot-scope="scope">
          <i class="el-icon-success" style="color: #67c23a" v-if="!scope.row.cat_deleted"></i>
          <i class="el-icon-error" style="color: red" v-else></i>
        </template>
        <template slot="order" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level === 0" type="primary" size="small">一级</el-tag>
          <el-tag v-else-if="scope.row.cat_level === 1" type="success" size="small">二级</el-tag>
          <el-tag v-else type="warning" size="small">三级</el-tag>
        </template>
        <template slot="opt">
          <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
        </template>
      </tree-table>

      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum" :page-sizes="[1, 3, 5, 10]" :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%" @close="addCateDialogClosed">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px" >
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类:">
          <el-cascader v-model="selectedKeys" :options="parentCateList" expand-trigger="hover" :props="cascaderProps"
            @change="parentCateChange" change-on-select clearable>
          </el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import goods from '@/api/goods'

export default {
  name: 'MyCate',
  data () {
    return {
      cateList: [],
      parentCateList: [],
      queryInfo: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      selectedKeys: [],
      addCateDialogVisible: false,
      addCateForm: {
        cat_name: '',
        cat_pid: 0,
        // 默认是一级分类
        cat_level: 0
      },
      addCateFormRules: {
        cat_name: [{ required: true, message: '请输入分类名称', trigger: 'blur' }]
      },
      total: 0,
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isok'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ]
    }
  },
  components: {
  },
  methods: {
    // 1.获取商品分类列表
    async getCategories () {
      const { data: res } = await goods.getCategories(this.queryInfo)
      if (res.meta.status === 200) {
        this.cateList = res.data.result
        this.total = res.data.total
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 2.切换页码大小
    handleSizeChange (val) {
      this.queryInfo.pagesize = val
      this.getCategories()
    },

    // 3.切换当前页码值
    handleCurrentChange (val) {
      this.queryInfo.pagenum = val
      this.getCategories()
    },

    // 4.添加分类
    showAddCateDialog () {
      this.addCateDialogVisible = true
    },

    // 5.获取父级分类列表
    async getParentCateList () {
      const { data: res } = await goods.getCategories({ type: 2 })
      if (res.meta.status === 200) {
        this.parentCateList = res.data
      } else {
        this.$message.error(res.meta.msg)
      }
    },

    // 6.级联选择器发生改变
    parentCateChange () {
      const len = this.selectedKeys.length
      if (len > 0) {
        this.addCateForm.cat_pid = this.selectedKeys[len - 1]
        this.addCateForm.cat_level = len
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },

    // 7. 监听关闭对话框,重置表单数据
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectedKeys = []
      this.addCateForm.cat_pid = 0
      this.addCateForm.cat_level = 0
    },

    // 8.确认添加分类
    addCate () {
      this.$refs.addCateFormRef.validate(async valid => {
        if (valid) {
          const { data: res } = await goods.addCategory(this.addCateForm)
          if (res.meta.status === 201) {
            this.addCateDialogVisible = false
            this.getCategories()
            this.getParentCateList()
            this.$message.success(res.meta.msg)
          } else {
            this.addCateDialogVisible = false
            this.$message.error(res.meta.msg)
          }
        } else {
          return false
        }
      })
    }
  },
  created () {
    this.getCategories()
    this.getParentCateList()
  },
  mounted () {
  }
}
</script>

<style lang='less' scoped>
.el-breadcrumb {
  line-height: 50px;
}

.zk-table {
  margin: 15px 0;
  font-size: 14px;
}

.el-cascader {
  width: 100%
}
</style>
